package com.tiyende.kash.loop.utils.helper

import android.content.Context
import com.tiyende.kash.loop.ui.activity.TiyendeScoreActivity
import com.tiyende.kash.loop.utils.shared.TiyendeSharedHelper
import com.tiyende.kash.loop.utils.shared.TiyendeSharedKey
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/13 16:26
 * @Annotation：评分帮助类
 */
object TiyendeScoreDialogHelper {

    /**
     * 展示评分
     */
    fun showStarDialog(context: Context, data: JSONObject, isNeedAdd: Boolean){

        if (data.has("activeWindow") && data.optJSONObject("activeWindow") != null){
            var isShowActiveDialog = false
            val activeWindowObj = data.optJSONObject("activeWindow")
            activeWindowObj?.let {
                //需要弹出
                if (it.optString("switch") == "YES"){
                    if (isNeedAdd){//是否需要判断出现次数
                        val totalNum = it.optInt("num")
                        val curtNum = TiyendeSharedHelper.queryTiyendeSharedInt(TiyendeSharedKey.TIYENDE_DIALOG_ACTIVE_NUM)
                        if (curtNum < totalNum){
                            isShowActiveDialog = true
                            TiyendeSharedHelper.addTiyendeSharedData(TiyendeSharedKey.TIYENDE_DIALOG_ACTIVE_NUM,curtNum+1)
                        }
                    } else {
                        isShowActiveDialog = true
                    }
                }
                if (isShowActiveDialog){
                    showActiveStarDialog(context,it.optString("content"))
                    return
                }
            }
        }

        if (data.has("normalWindow") && data.optJSONObject("normalWindow") != null){
            val normalWindowObj = data.optJSONObject("normalWindow")
            normalWindowObj?.let {
                if (isNeedAdd){
                    if (it.optString("switch") == "YES"){
                        val totalNum = it.optInt("num")
                        val curtNum = TiyendeSharedHelper.queryTiyendeSharedInt(TiyendeSharedKey.TIYENDE_DIALOG_NORMAL_NUM)
                        if (curtNum >= totalNum){
                            return
                        }
                        TiyendeSharedHelper.addTiyendeSharedData(TiyendeSharedKey.TIYENDE_DIALOG_NORMAL_NUM,curtNum+1)
                        showNormalStarDialog(context,it.optString("content"))
                    }
                } else {//个人中心
                    showNormalStarDialog(context,it.optString("content"))
                }
            }
        }
    }

    /**
     * activeWindow
     */
    private fun showActiveStarDialog(context: Context, tips: String){
        TiyendeScoreActivity.start(context,tips)
    }

    /**
     * normalWindow
     */
    private fun showNormalStarDialog(context: Context, tips: String){
        TiyendeScoreActivity.start(context,tips)
    }
}